package cn.esbot.wx.mp.utils;

import org.springframework.http.HttpMethod;
import org.springframework.util.StreamUtils;
import org.springframework.web.client.RestTemplate;

import java.io.File;
import java.io.FileOutputStream;

public class FileUtils {
    public static File downloadFile(String url) {
        RestTemplate restTemplate = new RestTemplate();
        File file = restTemplate.execute(url, HttpMethod.GET, null, clientHttpResponse -> {
            String suffix = "." + clientHttpResponse.getHeaders().getContentType().getSubtype();
            File ret = File.createTempFile("download", suffix);
            StreamUtils.copy(clientHttpResponse.getBody(), new FileOutputStream(ret));
            return ret;
        });
        return file;
    }

}
